package core.tool;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.io.IOException;

import jade.content.ContentException;
import jade.content.lang.xml.XMLManager;
import jade.util.Logger;

import core.datamodel.Element;
import core.datamodel.Event;
import core.datamodel.Accountit;
import core.datamodel.Resource;

public class LoadEvents {

	private Logger log = Logger.getJADELogger(this.getClass().getName());
	private String pathXmlFile="input_cordys/";
	private String pathXmlFileOld=pathXmlFile+"old/";
	private File file;

	public LoadEvents() {
		file = new File(pathXmlFile);
	}



	public Accountit getEvents() {
		Accountit events = null;

		File[] fileList = file.listFiles(new FileEventFilter());
		if (fileList!= null  && fileList.length >0){
			for (File file : fileList){
				log.info("Elaboro  file:" +file.getName());

				try {

					XMLManager xmgr = new XMLManager();

					xmgr.add(Accountit.class);
					xmgr.add(Event.class);
					xmgr.add(Resource.class);
					xmgr.add(Element.class);
					events = (Accountit) xmgr.decode(file);
				
				}
				catch (ContentException ce) {
					ce.printStackTrace();
				}
				catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				// devo storicizzare il file xml
				file.renameTo(new File(pathXmlFileOld+file.getName()));
			} // end for
		}
		else {
			log.info("Nessun file eventi e' presente");
		}

		return events;
	}


}
